package com.lianzai.system.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.lianzai.camera.service.CameraBlackAndWhiteService;
import com.lianzai.stat.service.StatService;
import com.lianzai.util.TimeUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lianzai.common.annotation.Log;
import com.lianzai.common.controller.BaseController;
import com.lianzai.common.domain.FileDO;
import com.lianzai.common.domain.Tree;
import com.lianzai.common.service.FileService;
import com.lianzai.common.utils.MD5Utils;
import com.lianzai.common.utils.R;
import com.lianzai.common.utils.ShiroUtils;
import com.lianzai.system.domain.MenuDO;
import com.lianzai.system.service.MenuService;

@Controller

public class LoginController extends BaseController {

	@Autowired
	MenuService menuService;
	
	@Autowired
	FileService fileService;

	@Autowired
	private CameraBlackAndWhiteService cameraBlackAndWhiteService;

	@Autowired
	private StatService statService;

	@GetMapping({ "/", ""})
	String welcome(HttpServletRequest request,Model model) {
		return "login";
		
	}

	@GetMapping("/stat")
	String stat(Model model) {
		List<Map> blackList = cameraBlackAndWhiteService.allList();//查询黑名单
		Map compositeMap = statService.queryCompositeStat();		//查询综合指数
		List<Map> parkingList = statService.queryParkingStat();		//查询停车场进度
		List<Map> mapList = statService.queryCommunityMapList();		//地图数据
		Map userMap = statService.queryUserTotal();		//查询用户总数
		//查询当前月度小区排行
		Map dateMap = new HashMap();
		dateMap.put("start",TimeUtil.queryNowMonth()+"-01");
		dateMap.put("end",TimeUtil.queryNowMonth()+"-" + TimeUtil.getMontLhastDay());
		List<Map> communityFeeList = statService.queryCommunityFeeByMonth(dateMap);
		dateMap.clear();
		dateMap.put("start",TimeUtil.queryNowYear()+"-01-01");
		dateMap.put("end",TimeUtil.queryNowYear()+"-12-31");
		Map yearFeeMap = statService.queryYearFeeMap(dateMap);		//查询当前年度总收缴费用
		//统计今年每月的数据
		int nowMonth =TimeUtil.queryMonth();
		List<Map> monthFeeList = new ArrayList<>();
		for(int i=1;i<=nowMonth;i++){
			String yearMonth = TimeUtil.queryNowYear();
			String month = "01月";
			if(nowMonth<10){
				yearMonth = yearMonth + "-0" + i;
				month = "0" + i + "月";
			}else{
				yearMonth = yearMonth + "-" + i;
				month = i + "月";
			}
			dateMap.clear();
			dateMap.put("start",yearMonth+"-01");
			dateMap.put("end",yearMonth+"-"+TimeUtil.getMontLhasDay(TimeUtil.queryNowYearInt(),i));
			Map monthFeeMap = statService.queryMonthFeeMap(dateMap);		//查询月度收缴费用
			if(monthFeeMap == null){
				monthFeeMap = new HashMap();
				monthFeeMap.put("sun_fee",0);
			}
			monthFeeMap.put("month",month);
			monthFeeList.add(monthFeeMap);
		}

		model.addAttribute("blackList", blackList);
		model.addAttribute("compositeMap", JSON.toJSONString(compositeMap));
		model.addAttribute("parkingList", parkingList);
		model.addAttribute("mapList", JSON.toJSONString(mapList));
		model.addAttribute("userMap", userMap);
		model.addAttribute("communityFeeList", communityFeeList);
		model.addAttribute("yearFeeMap", yearFeeMap);
		model.addAttribute("monthFeeList", JSON.toJSONString(monthFeeList));
		return "stat";
	}

	@GetMapping("/tool")
	String tool(Model model) {
		return "tool";
	}

	@Log("请求访问主页")
	@GetMapping({ "/index" })
	String index(Model model,HttpServletRequest request) {
		List<Tree<MenuDO>> menus = menuService.listMenuTree(getUserId());
		model.addAttribute("menus", menus);
		model.addAttribute("name", getUser().getName());
		String tab = request.getParameter("tab");
		FileDO fileDO = fileService.get(getUser().getPicId());
		if(fileDO!=null&&fileDO.getUrl()!=null){
			if(fileService.isExist(fileDO.getUrl())){
				model.addAttribute("picUrl",fileDO.getUrl());
			}else {
				model.addAttribute("picUrl","/img/photo_s.jpg");
			}
		}else {
			model.addAttribute("picUrl","/img/photo_s.jpg");
		}
		model.addAttribute("username", getUser().getUsername());
		model.addAttribute("tab", tab);
		return "index";
	}

	@GetMapping("/login")
	String login() {
		return "login";
	}

	@Log("登录")
	@PostMapping("/login")
	@ResponseBody
	R ajaxLogin(String username, String password) {

		password = MD5Utils.encrypt(username, password);
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			return R.ok();
		} catch (AuthenticationException e) {
			return R.error("用户或密码错误");
		}
	}

	@GetMapping("/logout")
	String logout() {
		ShiroUtils.logout();
		return "redirect:/login";
	}

	@GetMapping("/main")
	String main() {
		return "main";
	}

}
